package in.omerjerk.fullscreenimmersive;

import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.ActivityChooserView;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.Toast;
import com.facebook.ads.Ad;
import com.facebook.ads.AdError;
import com.facebook.ads.InterstitialAd;
import com.facebook.ads.InterstitialAdListener;

public class MainActivity extends AppCompatActivity {
    private static final int MODE_HIDE_ALL = 2;
    private static final int MODE_HIDE_NAV_BAR = 1;
    private static final int MODE_HIDE_NONE = 0;
    private static final String PLACEMENT_ID = "2183706955286436_2183707105286421";
    private static final String PREF_MODE = "mode";
    private static final int REQUEST_OVERLAY_PERMISSION = 1;
    /* access modifiers changed from: private */
    public static final String TAG = "MainActivity";
    private RadioButton hideAllRadio;
    private RadioButton hideNavBarRadio;
    private RadioButton hideNoneRadio;
    private InterstitialAd interstitialAd;
    int mode = 0;
    private RadioGroup modeType;
    /* access modifiers changed from: private */
    public Switch serviceSwitch;

    /* access modifiers changed from: protected */
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView((int) R.layout.activity_main);
        this.interstitialAd = new InterstitialAd(this, PLACEMENT_ID);
        this.serviceSwitch = (Switch) findViewById(R.id.service_switch);
        this.modeType = (RadioGroup) findViewById(R.id.radio_group);
        this.hideNoneRadio = (RadioButton) findViewById(R.id.radio_none);
        this.hideNavBarRadio = (RadioButton) findViewById(R.id.radio_hide_nav_bar);
        this.hideAllRadio = (RadioButton) findViewById(R.id.radio_hide_all);
        this.modeType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                MainActivity.this.setMode();
                if (MainActivity.this.serviceSwitch.isChecked()) {
                    MainActivity.this.startImmersiveService(MainActivity.this.mode);
                }
            }
        });
        this.serviceSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton compoundButton, boolean z) {
                if (!z) {
                    MainActivity.this.mode = 0;
                    MainActivity.this.stopService(new Intent(MainActivity.this, ImmersiveService.class));
                    return;
                }
                MainActivity.this.setMode();
                MainActivity.this.startImmersiveService(MainActivity.this.mode);
            }
        });
        initState();
        initAds();
    }

    @TargetApi(23)
    public void onActivityResult(int i, int i2, Intent intent) {
        if (i != 1) {
            return;
        }
        if (!Settings.canDrawOverlays(this)) {
            showToast("You need to give \"Draw over other apps\" permission for this app to work.");
        } else if (this.serviceSwitch.isChecked()) {
            startImmersiveService(this.mode);
        }
    }

    private void initState() {
        this.mode = PreferenceManager.getDefaultSharedPreferences(this).getInt("mode", 0);
        switch (this.mode) {
            case 1:
                this.hideNavBarRadio.setChecked(true);
                break;
            case 2:
                this.hideAllRadio.setChecked(true);
                break;
            default:
                this.hideNoneRadio.setChecked(true);
                break;
        }
        for (ActivityManager.RunningServiceInfo runningServiceInfo : ((ActivityManager) getSystemService("activity")).getRunningServices(ActivityChooserView.ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED)) {
            if (ImmersiveService.class.getName().equals(runningServiceInfo.service.getClassName())) {
                this.serviceSwitch.setChecked(true);
            }
        }
    }

    /* access modifiers changed from: private */
    public void setMode() {
        if (this.hideNoneRadio.isChecked()) {
            this.mode = 1;
            updatePreference(0);
        } else if (this.hideNavBarRadio.isChecked()) {
            this.mode = 2;
            updatePreference(1);
        } else if (this.hideAllRadio.isChecked()) {
            this.mode = 6;
            updatePreference(2);
        }
    }

    /* access modifiers changed from: private */
    public void startImmersiveService(int i) {
        if (Build.VERSION.SDK_INT < 23 || Settings.canDrawOverlays(this)) {
            Intent intent = new Intent(this, ImmersiveService.class);
            intent.putExtra("mode", i);
            startService(intent);
            return;
        }
        new SystemOverlayPermissionDialog().show(getFragmentManager(), "System Overlay Dialog");
    }

    private void updatePreference(int i) {
        SharedPreferences.Editor edit = PreferenceManager.getDefaultSharedPreferences(this).edit();
        edit.putInt("mode", i);
        edit.commit();
    }

    public void requestSystemAlertPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            String packageName = getPackageName();
            startActivityForResult(new Intent("android.settings.action.MANAGE_OVERLAY_PERMISSION", Uri.parse("package:" + packageName)), 1);
        }
    }

    private void showToast(final String str) {
        runOnUiThread(new Runnable() {
            public void run() {
                Toast.makeText(MainActivity.this, str, 0).show();
            }
        });
    }

    private void initAds() {
        this.interstitialAd.setAdListener(new InterstitialAdListener() {
            public void onAdClicked(Ad ad) {
            }

            public void onInterstitialDismissed(Ad ad) {
            }

            public void onLoggingImpression(Ad ad) {
            }

            public void onInterstitialDisplayed(Ad ad) {
                Log.e(MainActivity.TAG, "Ad displayed");
            }

            public void onError(Ad ad, AdError adError) {
                String access$300 = MainActivity.TAG;
                Log.e(access$300, "Ad load error = " + adError.getErrorMessage());
            }

            public void onAdLoaded(Ad ad) {
                Log.e(MainActivity.TAG, "Ad loaded");
            }
        });
        this.interstitialAd.loadAd();
    }

    public void onStop() {
        if (this.interstitialAd != null && this.interstitialAd.isAdLoaded() && !this.interstitialAd.isAdInvalidated()) {
            this.interstitialAd.show();
            this.interstitialAd.loadAd();
        }
        super.onStop();
    }
}
